package com.fc.app.fcapp.pages.overdraw;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fc.app.fcapp.R;
import com.fc.app.fcapp.pages.jsondata.HttpMethods;

import com.fc.library.fresco.Util;
import com.fc.library.imageloader.ImageLoader;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by fc on 2017/10/26.
 */

public class OverDrawActivity extends Activity {
    private ListView mListView;
    private LayoutInflater mInflater;
    View view=null;
    LinearLayout mainView =null;
    private Button jsonButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.overdraw_list);
        mainView = (LinearLayout) findViewById(R.id.parent);
        jsonButton = (Button) findViewById(R.id.jsonbutton);
        jsonButton.setOnClickListener(clickListener);

        view = ImageLoader.getLoader().getView(getApplicationContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(Util.Div(300),Util.Div(150));
        mainView.addView(view,lp);
        loadImg();
        mInflater = LayoutInflater.from(this);

        Util.instence(this);


        mListView = (ListView) findViewById(R.id.id_listview_chats);

        mListView.setAdapter(new ArrayAdapter<Droid>(this, -1, Droid.generateDatas()) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ViewHolder holder = null;
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.overdraw_item, parent, false);
                    holder = new ViewHolder();
                    holder.icon = (ImageView) convertView.findViewById(R.id.id_chat_icon);
                    holder.name = (TextView) convertView.findViewById(R.id.id_chat_name);
                    holder.date = (TextView) convertView.findViewById(R.id.id_chat_date);
                    holder.msg = (TextView) convertView.findViewById(R.id.id_chat_msg);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                Droid droid = getItem(position);
                holder.icon.setBackgroundColor(0x44ff0000);
                holder.icon.setImageResource(droid.imageId);
                holder.date.setText(droid.date);
                holder.msg.setText(droid.msg);
                holder.name.setText(droid.name);

                return convertView;
            }

            class ViewHolder {
                ImageView icon;
                TextView name;
                TextView date;
                TextView msg;
            }
        });
    }

    private void loadImg() {

        String url = "http://img.coocaa.com/java-manage/images/20171109/1510212910236318186490_original_img.jpg";

        ImageLoader.getLoader().with(getApplicationContext()).
                load(Uri.parse(url)).wrapContent().into(view);
//
    }

    public View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"jsonButton",Toast.LENGTH_SHORT).show();
            scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
            scheduledExecutorService.scheduleWithFixedDelay(new Task(), 0,4, TimeUnit.SECONDS);

        }
    };

    private ScheduledExecutorService scheduledExecutorService;

    private class Task implements Runnable{

        @Override
        public void run() {
            HttpMethods.postData(HttpMethods.getPkgMsgs());
        }
    }

}
